<script setup>
import {ref} from "vue"
import { httpRequest } from "../../utils/http"

const count = ref(0)
count.value = 30*60
const code = ref(0)
const timeId = ref(null)
const num = ref(0)
num.value = uni.getStorageSync('shop')[uni.getStorageSync('index')].ka?uni.getStorageSync('shop')[uni.getStorageSync('index')].ka.quanNum:0
const getCode = async() => {
    uni.showLoading({
        title: '请稍后',
        mask: false
    })
    const res = await httpRequest('/api/getEmpowerShopCode','POST',{shopId:uni.getStorageSync('shop')[uni.getStorageSync('index')].shopId})
    code.value = res.data
    timeId.value = setInterval(countDown, 1000);
    uni.setStorageSync('code',res.data)
    uni.hideLoading()
}
const accountList = ref([])
const getAccount = async() => {
    uni.showLoading({
        title: '请稍后',
        mask: false
    })
    const res = await httpRequest('/api/getShopEmpowerBusiness','POST',{shopId:uni.getStorageSync('shop')[uni.getStorageSync('index')].shopId})
    uni.hideLoading()
    accountList.value = res.data
}
const time = ref(0)
const countDown = () => {
    const m = Math.floor(count.value/60)
    const s = count.value % 60
    time.value = `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`
    if(count.value == 0){
        clearInterval(timeId.value)
    }
    count.value--
}
countDown()
const copy = () => {
    uni.setClipboardData({
        data: code.value,
        success: (result) => {
            uni.showToast({
                title: '复制成功',
                icon: 'none'
            })
        },
        fail: (error) => {
            uni.showToast({
                title: '复制失败',
                icon: 'none'
            })
        }
    })
}
getCode()
getAccount()
const relieve = async (i) => {
    uni.showModal({
        title: '提示',
        content: '确定要解除授权吗?',
        success: async(res) => {
            if(res.confirm){
                const res = await httpRequest('/api/delShopEmpower','POST',{
                    shopId:uni.getStorageSync('shop')[uni.getStorageSync('index')].shopId,
                    businessId:accountList.value[i].businessId
                })
                console.log(res);
                if(res.success){
                    uni.showToast({
                        title: '删除成功',
                        icon: 'none'
                    })
                }else{
                    uni.showToast({
                        title: '删除失败',
                        icon: 'none'
                    })
                }
                getAccount()
            }
        }
    })
    
}

</script>

<template>
  <view style="min-height: 1500rpx;background-color: #f6f8fb;">
    <view class="page">
    <view class="code" v-if="accountList.length<num">
        <text class="yard">授权码：{{ code }}</text>
        <text v-if="count" class="lose">授权码失效还剩<text style="color: rgb(255,55,71);">{{time}}</text></text>
        <text v-else class="lose">已失效</text>
        <view v-if="count" class="copy" @click="copy">一键复制授权码</view>
        <view v-else class="copy">
            <image style="width: 32rpx;height: 32rpx;" src="" mode="" />
            <text style="color: rgb(69,70,74);" @click="getCode">重新获取</text>
        </view>
    </view>
    <view class="manage">
        <view class="manage-text">管理已授权的账号({{accountList.length}})</view>
        <view class="id" v-for="item,index in accountList" :key="index">
            <view style="display: flex;align-items: center;font-size: 28rpx;">
                <image style="width: 64rpx;height: 64rpx;margin-right: 10rpx;" :src="item.businessImg||'https://www.diandianyouke.site:801/fileDataBase/static/stag/static/img/user_icon_HeadSculpture.png'" mode="" />
                <text>{{ item.weChatName }}</text>
            </view>
            <view @click="relieve(index)" class="relieve">解除授权</view>
        </view>
    </view>
    <text class="max">{{`当前版本最多能授权${num}个账号`}}</text>
  </view>
  </view>
</template>

<style lang="scss" scoped>
    .page{
        padding: 30rpx;
        .code{
            display: flex;
            flex-direction: column;
            padding: 36rpx;
            align-items: center;
            width: 100%;
            height: 302rpx;
            background-color: #fff;
            border-radius: 24rpx;
            box-sizing: border-box;
            .yard{
                font-size: 32rpx;
                color: rgb(48,49,51);
                margin-bottom: 18rpx;
                font-weight: 700;
            }
            .lose{
                font-size: 24rpx;
                color: rgb(144,147,153);
                margin-bottom: 48rpx;
            }
            .copy{
                width: 414rpx;
                height: 80rpx;
                background-color: rgb(142,84,244);
                border-radius: 432rpx;
                color: #fff;
                font-size: 28rpx;
                display: flex;
                justify-content: center;
                align-items: center;
            }
        }

        .manage{
            width: 100%;
            padding: 36rpx 30rpx;
            background-color: #fff;
            border-radius: 24rpx;
            box-sizing: border-box;
            margin-top: 30rpx;
            .manage-text{
                font-size: 32rpx;
                font-weight: 500;
                color: rgb(69,70,74);
            }
            .id{
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-top: 36rpx;
                .relieve{
                    width: 172rpx;
                    height: 56rpx;
                    text-align: center;
                    line-height: 56rpx;
                    border: 1px solid rgb(220,223,230);
                    font-size: 28rpx;
                    color: rgb(144,147,153);
                    border-radius:180rpx
                }
            }
        }
        .max{
            display: flex;
            justify-content: center;
            margin-top: 30rpx;
            font-size: 24rpx;
            color: rgb(255,55,71)
        }
    }
</style>
